<?php

/**
 * Created by PhpStorm.
 * User: TC
 * Date: 2016/3/25
 * Time: 15:01
 * 通告
 */
require_once dirname(__FILE__) . '/../Config.php';
require_once dirname(__FILE__) . '/../libs/controller/BriefController.php';

$brief = new brief();
$brief->Router();

class brief
{
  /*
   * 路由
   */
  public function Router()
  {
    $action = isset($_GET[Constant::ACTION_FIELD]) ? $_GET[Constant::ACTION_FIELD] : '';
    switch ($action) {
      case 'add':
        //添加简报
        $this->AddBrief();
        break;
      case 'remove':
        //删除简报
        $this->RemoveBrief();
        break;
      case 'list':
        //简报列表
        $this->BriefList();
        break;
      case 'modifyWeight':
        //修改排序权重
        $this->ModifyWeight();
        break;
      default:
        ResponseMsg::ResponseErrorMsg(404);
        break;
    }
  }

  /*
   * 添加简报
   */
  private function AddBrief()
  {
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
      ResponseMsg::ResponseErrorMsg(404);
      return;
    }
    $data = Util::TranInputData();
    if(!isset($data[BriefModel::TYPE_FIELD])||(!$data[BriefModel::TYPE_FIELD])){
      ResponseMsg::ResponseErrorMsg(400,'缺少类型参数');
      return;
    }
    if(!isset($data[BriefModel::IMG_IFELD])||(!$data[BriefModel::IMG_IFELD])){
      ResponseMsg::ResponseErrorMsg(400,'缺少图片');
      return;
    }
    if(!isset($data[BriefModel::TITLE_FIELD])||(!$data[BriefModel::TITLE_FIELD])){
      ResponseMsg::ResponseErrorMsg(400,'缺少标题');
      return;
    }
    if(!isset($data[BriefModel::CONTENT_FIELD])||(!$data[BriefModel::CONTENT_FIELD])){
      ResponseMsg::ResponseErrorMsg(400,'缺少内容');
      return;
    }
    $type = $data[BriefModel::TYPE_FIELD];
    $img = $data[BriefModel::IMG_IFELD];
    $title = $data[BriefModel::TITLE_FIELD];
    $content = $data[BriefModel::CONTENT_FIELD];
    $author = "小八";
    $result = BriefController::AddBrief($type,$img,$title,$content,$author);
    if(!$result){
      ResponseMsg::ResponseErrorMsg(400,'发布失败');
      return;
    }
    $brief = BriefController::FindBriefById($result);
    ResponseMsg::ResponseContent(['brief'=>$brief],'success');
    return;
  }

  /*
   * 删除简报
   */
  private function RemoveBrief()
  {
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
      ResponseMsg::ResponseErrorMsg(404);
      return;
    }
    $data = Util::TranInputData();
    $id = isset($data[Constant::ID_FIELD]) ? $data[Constant::ID_FIELD] : "";
    if (!$id) {
      ResponseMsg::ResponseErrorMsg(400, '缺少id');
      return;
    }
    $result = BriefController::Remove($id);
    $msg = $result ? [200, '删除成功'] : [400, '删除失败'];
    ResponseMsg::ResponseErrorMsgArr($msg);
    return;
  }

  /*
   * 简报列表
   */
  private function BriefList()
  {
    if ($_SERVER['REQUEST_METHOD'] != 'GET') {
      ResponseMsg::ResponseErrorMsg(404);
      return;
    }
    $page = isset($_GET[Constant::PAGE_FIELD]) ? $_GET[Constant::PAGE_FIELD] : 1;
    $type = isset($_GET[Constant::TYPE_FIELD]) ? $_GET[Constant::TYPE_FIELD] : 1;
    if ($type == 1) {
      //按照时间排序获取
      $briefList = BriefController::BriefListByTime($page, 20, 1);
    } else {
      //按照权重值排序获取
      $briefList = BriefController::BriefListByWeight($page, 20);
    }
    ResponseMsg::ResponseContent([Constant::PAGE_FIELD => $page, 'data' => $briefList], 'success');
    return;
  }

  /*
   * 修改排序
   */
  private function ModifyWeight()
  {
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
      ResponseMsg::ResponseErrorMsg(404);
      return;
    }
    $data = Util::TranInputData();
    $id = isset($data[Constant::ID_FIELD]) ? $data[Constant::ID_FIELD] : '';
    if (!$id) {
      ResponseMsg::ResponseErrorMsg(400, '缺少id');
      return;
    }
    $weight = isset($data[Constant::WEIGHT_FIELD]) ? $data[Constant::WEIGHT_FIELD] : -1;
    if ($weight < 0) {
      ResponseMsg::ResponseErrorMsg(400, '权重值不合法');
      return;
    }
    $result = BriefController::ModifyWeight($id, $weight);
    if ($result) {
      ResponseMsg::ResponseErrorMsg(200, '修改成功');
      return;
    } else {
      ResponseMsg::ResponseErrorMsg(400, '修改失败');
      return;
    }
  }
}